package com.software.shared;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    private EditText edt_password;
    private Button btn_login;
    private EditText edt_username;
    private CheckBox cb_auto_login;
    private CheckBox cb_remember;
    private SharedPreferences preferences;
    private Button btn_save;
    private Button btn_sd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
        initEvents();
        initShared();
        // 一定要在initShared下面
        initData();

    }

    private void initData() {
        // 第二个参数，如果没有在shared获取到remember，返回一个默认值，防止空指针
        boolean remember = preferences.getBoolean("remember", false);
        boolean autoLogin = preferences.getBoolean("auto_login", false);
        // 设置复选框的选中状态
        cb_remember.setChecked(remember);
        cb_auto_login.setChecked(autoLogin);
        //如果选中了记住我
        if (remember) {
            // 回显数据
            edt_username.setText(preferences.getString("username", ""));
            edt_password.setText(preferences.getString("password", ""));
        }
        //如果选中了自动登录
        if (autoLogin) {
            // 模拟登录逻辑，做个提示框
            Toast.makeText(this, "自动登录成功", Toast.LENGTH_SHORT).show();
        }
    }

    private void initShared() {
        // 如何获取 SharedPreferences
        preferences = getSharedPreferences("config", Context.MODE_PRIVATE);
    }

    private void initEvents() {

        btn_login.setOnClickListener(v -> {
            //点击登录按钮以后的逻辑
            SharedPreferences.Editor editor = preferences.edit();
            // 存储是否选中了记住我
            editor.putBoolean("remember", cb_remember.isChecked());
            // 存储是否选中了自动登录
            editor.putBoolean("auto_login", cb_auto_login.isChecked());
            if (cb_remember.isChecked()) {
                editor.putString("username",edt_username.getText().toString());
                editor.putString("password",edt_password.getText().toString());
            }
            // 提交保存、同步，需要得到返回结果执行，执行出错，应用出错
            editor.commit();
            //异步保存，不需要得到返回结果时调用， 起了另一个线程，保存执行出错，不影响运行
            //editor.apply();
            Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
        });

        btn_save.setOnClickListener(v -> {
            try {
                FileOutputStream fos = openFileOutput("happy.txt", Context.MODE_PRIVATE);
                fos.write("天天通天塔".getBytes());
                fos.flush();
                fos.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            } finally {
                Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
            }
        });


        btn_sd.setOnClickListener(v -> {
            String storageState = Environment.getExternalStorageState();
            //确定有SD卡存在
            if (storageState.equals(Environment.MEDIA_MOUNTED)) {
                // 获取SD卡存文件的目录，并自定义txtFile目录
                String path = getExternalFilesDir(null).getAbsolutePath() + "/txtFile/";
                //  创建文件夹目录
                File file = new File(path);
                // 判断文件夹是否存在，不存在就创建
                if (!file.exists()) {
                    //新建文件夹
                    file.mkdir();
                }

                try {
                    FileOutputStream fos = new FileOutputStream(path + "test.txt");
                    fos.write("天天通天塔".getBytes());
                    fos.flush();
                    fos.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                } finally {
                    Toast.makeText(this, "写入SD卡成功", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void initViews() {
        edt_username = findViewById(R.id.edt_username);
        btn_login = findViewById(R.id.btn_login);
        edt_password = findViewById(R.id.edt_password);
        cb_remember = findViewById(R.id.cb_remember);
        cb_auto_login = findViewById(R.id.cb_auto_login);
        //读取应用程序内部文件
        btn_save = findViewById(R.id.btn_save);
        btn_sd = findViewById(R.id.btn_sd);
    }
}